package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;



    /**
     * 新增菜品
     *
     * @param dishDTO
     */
    @Transactional
    @Override
    public void addDish(DishDTO dishDTO) {


        /**
         * 插入一条菜品，基础表插入
         */
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setStatus(StatusConstant.DISABLE);
        dishMapper.insert(dish);


        dishDTO.getFlavors().forEach(dishFlavor -> {
            dishFlavor.setDishId(dish.getId());
        });
        dishFlavorMapper.insertBatch(dishDTO.getFlavors());

    }


    /**
     * 分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageDish(DishPageQueryDTO dishPageQueryDTO) {



        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishPageQueryDTO,dish);
        Page<Dish> dishes = dishMapper.PageQuery(dish);


        PageResult pageResult = new PageResult();
        pageResult.setRecords(dishes.getResult());
        pageResult.setTotal(dishes.getTotal());
        return pageResult;
    }


    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @Override
    public void deleteDish(List<Integer> ids) {
        ids.forEach(id->{
            DishVO dish = dishMapper.findById(id);
            if (dish.getStatus()==StatusConstant.ENABLE){
                throw new DeletionNotAllowedException("正在出售的菜品不允许被删除");
            }
        });


        try {
            dishMapper.deleteBatch(ids);
            dishFlavorMapper.deleteDishFlavorBatch(ids);
        } catch (Exception e) {
            throw new DeletionNotAllowedException("被套餐关联的菜品不允许被删除");
        }
    }


    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @Override
    public void modifyDish(DishDTO dishDTO) {
        /**
         * 更新菜品
         */
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);

        /**
         * 更新口味
         */
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(f->{
            f.setDishId(dish.getId());
        });

        ArrayList<Integer> ids = new ArrayList<>();
        ids.add(Math.toIntExact(dish.getId()));
        dishFlavorMapper.deleteDishFlavorBatch(ids);
        dishFlavorMapper.insertBatch(flavors);
    }

    @Override
    public DishVO findById(Integer id) {
        return dishMapper.findById(id);
    }

    @Override
    public List<Dish> findByCategory(Integer categoryId) {

        return dishMapper.findByCategory(categoryId);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.PageQuery(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
